package HelloSeries.ui.prefs.elems.flextables.editor;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;

import HelloSeries.ui.prefs.elems.flextables.model.IFlexTableModel;

public class ButtonEditor extends DefaultCellEditor {
	protected JButton button;
	private String    label;
	private boolean   isPushed;
	private JTable table;
	private int deleted_row;

	public ButtonEditor(JCheckBox checkBox) {
		super(checkBox);
		button = new JButton();
		button.setOpaque(true);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEditingStopped();
			}
		});
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		this.table = table;
		this.deleted_row = row;
		if (isSelected) {
			button.setForeground(table.getSelectionForeground());
			button.setBackground(table.getSelectionBackground());
		} else{
			button.setForeground(table.getForeground());
			button.setBackground(table.getBackground());
		}
		label = ((JButton) value).getText();
		button.setText(label);
		isPushed = true;
		return button;
	}

	public Object getCellEditorValue() {
		if (isPushed && label.equals("x"))  {
            ( (IFlexTableModel) table.getModel() ).removeRow( deleted_row );
		}
		else if (isPushed && label.equals("+")){
            ( (IFlexTableModel) table.getModel() ).addRow();
		}
		isPushed = false;
		return new String(label) ;
	}

	public boolean stopCellEditing() {
		isPushed = false;
		return super.stopCellEditing();
	}

	protected void fireEditingStopped() {
		super.fireEditingStopped();
	}
}